package cn.com.gridinfo.sooc.core.my.msg.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;

import com.jeremy.arad.utils.AnimUtil;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.my.commen.CodeConst;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.core.my.msg.adapter.CourseMsgAdapter;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 课程通知 详情
 * Created by liuxiaochen on 2016/1/6.
 */
public class CourseMsgActiity extends BaseSoocActivity {

    @InjectView(R.id.listView)
    UltimateRecyclerView course_msg_list;
    MyDao myDao;
    LinearLayoutManager linearLayoutManager;//列表管理器
    CourseMsgAdapter courseMsgAdapter;

    int pageNo = 1;
    int pageSize = 10;
    int maxLastPositon;//数据最后位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_msg_course_list);
        ButterKnife.inject(this);
        myDao = new MyDao(this);
        myDao.myCourseMsg(pageNo, pageSize, CodeConst.COURSEMSG_FIRST);//调用接口请求数据
        linearLayoutManager = new LinearLayoutManager(this);
        course_msg_list.setLayoutManager(linearLayoutManager);
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "课程通知";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //点击返回
    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        finish();
        AnimUtil.intentSlidOut(this);
    }

    /**
     * 请求成功
     */
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        try {
            if (myDao.status.equals("0")) {
                switch (requestCode) {
                    case CodeConst.COURSEMSG_FIRST:
                        initCourseMsg();
                        break;
                    //刷新操作
                    case CodeConst.COURSEMSG_PUSHDOWN:
                        course_msg_list.setRefreshing(false);
                        if (myDao.getCourseMsg() != null && !myDao.getCourseMsg().isEmpty()) {
                            course_msg_list.hideEmptyView();

                        } else {
                            //无数据处理
                            course_msg_list.showEmptyView();
                        }
                        //刷新数据
                        courseMsgAdapter.refresh(myDao.getCourseMsg());
                        if (myDao.getCourseMsg().size() > (pageSize - 1)) {
                            //数据大于每页显示的条数,则重新开启加载更多
                            courseMsgAdapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话则不显示加载更多
                            courseMsgAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    //课程翻页操作
                    case CodeConst.COURSEMSG_PULLUP:
                        if (myDao.getCourseMsg() != null && !myDao.getCourseMsg().isEmpty()) {
                            //加载更多
                            courseMsgAdapter.loadmore(myDao.getCourseMsg());
                        } else {
                            //无数据则不显示下一页
                            courseMsgAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, myDao.statusCode);
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    /**
     * 初始化 list 数据 分页
     */
    private void initCourseMsg() {
        courseMsgAdapter = new CourseMsgAdapter(myDao.getCourseMsg(), this);
        course_msg_list.setAdapter(courseMsgAdapter);
        course_msg_list.enableLoadmore();
        course_msg_list.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                //下拉刷新 课程通知
                pageNo = 1;//首页
                myDao.myCourseMsg(pageNo, pageSize, CodeConst.COURSEMSG_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        course_msg_list.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {

                if (itemsCount > (pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    myDao.myCourseMsg(++pageNo, pageSize, CodeConst.COURSEMSG_PULLUP);
                } else {
                    courseMsgAdapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }

        });
    }
}
